<?php

	class My_Validate_AllowedEmailDomain extends Zend_Validate_Abstract
	{
	    const EMAIL_DOMAIN_IS_NOT_ALLOWED = 'NotAllowedEmailDomain';

	    protected $_messageTemplates = array(
	        self::EMAIL_DOMAIN_IS_NOT_ALLOWED => "not allowed email domain"
	    );

	    public function isValid($value)
	    {

	    	//$this->_setValue((string) $value);
	    	$this->_setValue($value);

    		$siteConfig = Zend_Registry::get('siteConfig');
    		/*$allowedDomains = (array)$siteConfig->signup->allowedDomains->toArray();
    		//Zend_Debug::dump($allowedDomains);

	    	list($emailUserName, $emailDomain) = explode("@", $value);

			if (!in_array($emailDomain, $allowedDomains)) {
	            $this->_error(self::EMAIL_DOMAIN_IS_NOT_ALLOWED);
	            return false;
			}
            */

    		$allowedDomainExtension = (string)$siteConfig->signup->allowedDomainExtension;
            if (empty($allowedDomainExtension)) {
                throw new Exception('Signup Email Domain Parameter Error');
            }
            $posA = strpos($value, '.'.$allowedDomainExtension);
            $posB = strpos($value, '@'.$allowedDomainExtension);

			if ( ($posA === false) && ($posB === false) ) {
	            $this->_error(self::EMAIL_DOMAIN_IS_NOT_ALLOWED);
	            return false;
			}

			return true;

	    }

	}

